/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.abcd.godating.web.signup;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

import com.abcd.godating.domain.Account;
import com.abcd.godating.repository.jdbc.UsernameAlreadyInUseException;
import com.abcd.godating.service.AccountService;
import com.abcd.godating.service.RelationshipService;
import com.abcd.godating.utils.SecurityUtils;

@Controller
public class SignupController {

	@Inject
	private AccountService accountService;

	@Inject
	public RelationshipService relationshipService;

	private Account createAccount(Account account, BindingResult formBinding) {
		try {
			account = accountService.createAccount(account);
			relationshipService.creatAccount(account.getId());
			return account;
		} catch (UsernameAlreadyInUseException e) {
			formBinding.rejectValue("username", "user.duplicateUsername",
					"already in use");
			return null;
		}
	}

	@RequestMapping(value = "/signup", method = RequestMethod.POST)
	public String signup(HttpSession httpSession, @Valid Account account,
			BindingResult formBinding, WebRequest request) {
		if (formBinding.hasErrors()) {
			return null;
		}
		account = createAccount(account, formBinding);
		if (account != null) {
			SecurityUtils.signin(account);
			ProviderSignInUtils.handlePostSignUp("" + account.getId(), request);
			return "redirect:/";
		}
		return null;
	}

	@RequestMapping(value = "/signup", method = RequestMethod.GET)
	public Account signupForm(WebRequest request) {
		Connection<?> connection = ProviderSignInUtils.getConnection(request);
		if (connection != null) {
			request.setAttribute(
					"message",
					"Your "
							+ StringUtils.capitalize(connection.getKey()
									.getProviderId())
							+ " account is not associated with a Spring Social Showcase account. If you're new, please sign up.",
					RequestAttributes.SCOPE_REQUEST);
			return Account.fromProviderUser(connection.fetchUserProfile());
		} else {
			return new Account();
		}
	}

}
